package com.xdq.controller;

import com.xdq.bean.Address;
import com.xdq.bean.AddressNameCount;
import com.xdq.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author: 徐东强
 * @date: 2018/9/27 下午2:39
 * @description:
 **/
@RestController
@RequestMapping("/api/address")
public class AddressController {

    private final AddressService addressService;

    @Autowired
    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }

    @PostMapping("/save")
    public Address save(@RequestBody Address address){
        return this.addressService.save(address).orElse(null);
    }

    @GetMapping("/all")
    public List<Address> getAllAddress(){
        return this.addressService.getAll().orElse(Collections.emptyList());
    }

    @GetMapping("/one")
    public Address getOne(String id){
        return this.addressService.getOneById(id).orElse(null);
    }

    @GetMapping("/count-by-name")
    public List<AddressNameCount> countAddressByName(){
        return this.addressService.getCountByName();
    }
}
